﻿using Learun.Util.SqlSugar;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;
using Learun.Cache.Base;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：关联类型表
    /// </summary>
    public class ec_CableSetWireService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_CableSetWireEntity> _cableSetWireRepository => new Repository<ec_CableSetWireEntity>();
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_CableSetWireEntity> GetList(string projId, string cableId, string SetId)
        {
            try
            {
                var ProjectEntity = new ec_projectEntity();
                ProjectEntity = _projectRepository.GetById(projId);

                if (ProjectEntity == null)
                {
                    return null;
                }
                else
                {
                    var Res = SqlSugarHelper.Db.Queryable<ec_CableSetWireEntity>().AS("ec_cable_set_wire_" + ProjectEntity.ProjectIndex)
                    .InnerJoin<ec_CableSetEntity>((a, b) => a.CableSetID == b.CableSetID).AS<ec_CableSetEntity>("ec_cable_set_" + ProjectEntity.ProjectIndex)
                    .InnerJoin<ec_CableEntity>((a, b, c) => b.CableID == c.CableID).AS<ec_CableEntity>("ec_cable_" + ProjectEntity.ProjectIndex)
                    .WhereIF(!string.IsNullOrEmpty(cableId), (a, b, c) => c.CableID == cableId)
                    .WhereIF(!string.IsNullOrEmpty(SetId), (a, b, c) => b.CableSetID == SetId)
                    .OrderBy(a => a.SetLevel)
                    .ToList();
                    return Res; 
                } 
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, string keyValue, ec_CableSetWireEntity entity)
        {
            try
            {
                var ProjectEntity = new ec_projectEntity();
                if (!string.IsNullOrWhiteSpace(ProjectId))
                {
                    //ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    ProjectEntity = _projectRepository.GetById(ProjectId);
                }
                else
                {
                    return;
                }

                if (!string.IsNullOrEmpty(keyValue))
                {

                    entity.Modify(keyValue);
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        //this.BaseRepository().Update(entity);
                        _cableSetWireRepository.Update(entity);
                    }
                    else
                    {
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_Cable_set_wire_{ProjectEntity.ProjectIndex}", "WireID");
                        _cableSetWireRepository.AsUpdateable(entity).AS($"ec_Cable_set_wire_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }


                }
                else
                {
                    entity.Create();
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        //this.BaseRepository().Insert(entity);
                        _cableSetWireRepository.Insert(entity);
                    }
                    else
                    {
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_Cable_set_wire_{ProjectEntity.ProjectIndex}");
                        _cableSetWireRepository.AsInsertable(entity).AS($"ec_Cable_set_wire_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 删除实体数据
        /// <param name="ProjectId">主键</param>
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                var tableName = ProjectSugar.TableName<ec_CableSetWireEntity>(ProjectId);
                SqlSugarHelper.Db.Deleteable<ec_CableSetWireEntity>(x => x.WireID == keyValue).AS(tableName).ExecuteCommand();

                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存某个特定的属性（随着connection）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntityByConnections(string ProjectId, string keyValue, string wireTag, string polarity)
        {
            try
            {
                var ProjectEntity = new ec_projectEntity();
                if (!string.IsNullOrWhiteSpace(ProjectId))
                {
                    //ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    ProjectEntity = _projectRepository.GetById(ProjectId);
                }
                else
                {
                    return;
                }


                //var db = this.BaseRepository().BeginTrans();
                //db.ExecuteBySql($@" update ec_Cable_set_wire_{ProjectEntity.ProjectIndex} set wire_tag='{wireTag}', polarity='{polarity}' WHERE wireid ='{keyValue}'; ");

                //db.Commit();
                _cableSetWireRepository.AsTenant().BeginTran();
                SqlSugarHelper.Db.Ado.ExecuteCommand($@" update ec_Cable_set_wire_{ProjectEntity.ProjectIndex} set wire_tag='{wireTag}', polarity='{polarity}' WHERE wireid ='{keyValue}'; ");
                _cableSetWireRepository.AsTenant().CommitTran();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

    }
}
